#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

#define PLUGIN_VERSION "1.0"


#define PLUGIN_NAME                   "l4d2_Gas_Can_Teleport"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "当玩家使用油桶加油时，将地图上剩余的油桶传送到玩家脚下."   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}


// 油桶实体类名
#define GAS_CAN_CLASSNAME "weapon_gascan"

public void OnPluginStart()
{
    HookEvent("gascan_pour_completed", Event_GasCanPourCompleted);
}

public void Event_GasCanPourCompleted(Event event, const char[] name, bool dontBroadcast)
{
    int client = GetClientOfUserId(event.GetInt("userid"));
    
    if (!IsValidClient(client))
        return;
    
    // 获取玩家位置
    float origin[3];
    GetClientAbsOrigin(client, origin);
    
    // 稍微抬高一点，避免油桶卡在地面里
    origin[2] += 5.0;
    
    // 寻找地图上所有油桶
    int entity = -1;
    while ((entity = FindEntityByClassname(entity, GAS_CAN_CLASSNAME)) != -1)
    {
        // 跳过玩家手中的油桶
        if (GetEntPropEnt(entity, Prop_Send, "m_hOwnerEntity") == client)
            continue;
            
        // 传送油桶到玩家位置
        TeleportEntity(entity, origin, NULL_VECTOR, NULL_VECTOR);
        
        // 防止油桶堆叠在一起，稍微随机偏移一点
        origin[0] += GetRandomFloat(-10.0, 10.0);
        origin[1] += GetRandomFloat(-10.0, 10.0);
    }
}

bool IsValidClient(int client)
{
    return (client > 0 && client <= MaxClients && IsClientInGame(client) && IsPlayerAlive(client));
}